﻿package  {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.events.EventDispatcher;
	
	public class AbstractImageBox extends AbstractLoader{

		public var _imageBox:Sprite = new Sprite();
		public var _bmp:Bitmap;
		public var _boxWidth:Number;
		public var _boxHeight:Number;
		public var _imageWidthScale:Number;
		public var _imageHeightScale:Number;
		public var _container:Sprite;
		public var _id:String;
		
		public function AbstractImageBox(path:String,boxWidth:Number,boxHeight:Number,imageWidth:Number,imageHeight:Number,container:Sprite,id:String) {
			super(path);
			_boxWidth = boxWidth;
			_boxHeight = boxHeight;
			_imageWidthScale = _boxWidth/imageWidth;
			_imageHeightScale = _boxHeight/imageHeight;
			_container = container;
			_id = id;
		}
		
		override protected function onComplete(e:Event):void {
			_bmp = e.target.content as Bitmap;
			getImageBox();
			trace(typeof(_container)+"::"+typeof(this));
			_container.dispatchEvent(new Event(EventCollect.HAS_LOADED));
			_load.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			_load = null;
			//trace(_container.getChildAt(1).name);
		}
		public function getImageBox():void {
			
			this.graphics.beginBitmapFill (_bmp.bitmapData, new Matrix(_imageWidthScale,0,0,_imageHeightScale), false, true);
            this.graphics.drawRect (0, 0, _boxWidth, _boxHeight ); 
			this.graphics.endFill();
			_container.addChild(this);
			this.x = Math.random()*800;
			this.name = _id;
			//trace(this.parent.name);
		}

	}
	
}
